package cn.kimming.system.controller;

import cn.kimming.common.dto.EmployeeAddDTO;
import cn.kimming.common.dto.EmployeeUpdateDTO;
import cn.kimming.common.pojo.Employee;
import cn.kimming.common.util.Result;
import cn.kimming.common.vo.EmployeeVO;
import cn.kimming.service.IEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 刘铭轩
 * date 2021-05-23
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @GetMapping
    public Result findAll() {
        List<EmployeeVO> list = employeeService.findAll();
        return new Result(true, "查询成功").addData("list", list);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) {
        EmployeeVO employee = employeeService.findById(id);
        return new Result(true, "查询成功").addData("employee", employee);
    }

    @PostMapping
    public Result save(@Valid @RequestBody EmployeeAddDTO dto) {
        Employee bo = new Employee();
        BeanUtils.copyProperties(dto, bo);
        employeeService.save(bo);
        return new Result(true, "新增成功");
    }

    @PutMapping
    public Result update(@Valid @RequestBody EmployeeUpdateDTO dto) {
        Employee bo = new Employee();
        BeanUtils.copyProperties(dto, bo);
        employeeService.update(bo);
        return new Result(true, "更新成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        employeeService.deleteById(id);
        return new Result(true, "删除成功");
    }

}
